package com.qingcloud.common.auth.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qingcloud.common.auth.model.TokenVo;
import com.qingcloud.common.auth.service.ITokensService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * token管理接口
 */
@Api(tags = "Token管理")
@Slf4j
@RestController
@RequestMapping("/tokens")
public class TokensController {
    @Resource
    private ITokensService tokensService;

//    @Resource
//    private ClientDetailsService clientDetailsService;
//
//    @Resource
//    private PasswordEncoder passwordEncoder;

    @GetMapping("")
    @ApiOperation(value = "token列表")
    public Page<TokenVo> list(@RequestParam Map<String, Object> params, String tenantId) {
        return tokensService.listTokens(params, tenantId);
    }

//    @GetMapping("/key")
//    @ApiOperation(value = "获取jwt密钥")
//    public Result<String> key(HttpServletRequest request) {
//        try {
//            String[] clientArr = AuthUtils.extractClient(request);
//            ClientDetails clientDetails = clientDetailsService.loadClientByClientId(clientArr[0]);
//            if (clientDetails == null || !passwordEncoder.matches(clientArr[1], clientDetails.getClientSecret())) {
//                throw new BadCredentialsException("应用ID或密码错误");
//            }
//        } catch (AuthenticationException ae) {
//            return Result.fail(ae.getMessage());
//        }
//        org.springframework.core.io.Resource res = new ClassPathResource(SecurityConstants.RSA_PUBLIC_KEY);
//        try (BufferedReader br = new BufferedReader(new InputStreamReader(res.getInputStream()))) {
//            return Result.success(br.lines().collect(Collectors.joining("\n")));
//        } catch (IOException ioe) {
//            log.error("key error", ioe);
//            return Result.fail(ioe.getMessage());
//        }
//    }
}
